package com.inifitness.tenant.core.config.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * Redis 配置
 *
 * @author sunjinfeng
 * @since 2023/04/15
 */
@Configuration
public class RedisConfig {

  /**
   * 自定义 RedisTemplate
   * <p>
   * 修改 Redis 序列化方式，默认 JdkSerializationRedisSerializer
   *
   * @param redisConnectionFactory {@link RedisConnectionFactory}
   * @return {@link RedisTemplate}
   */
  @Bean
  public RedisTemplate<String, Object> redisTemplate(
      RedisConnectionFactory redisConnectionFactory) {

    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(redisConnectionFactory);

    redisTemplate.setKeySerializer(RedisSerializer.string());
    redisTemplate.setValueSerializer(RedisSerializer.json());

    redisTemplate.setHashKeySerializer(RedisSerializer.string());
    redisTemplate.setHashValueSerializer(RedisSerializer.json());

    redisTemplate.afterPropertiesSet();
    return redisTemplate;
  }

}
